<template>
  <!-- 内容 -->
  <view class="rateConfig">
    <!-- 刷卡费率 -->
    <view class="rateConfig_box">
      <view class="rateConfig_title">
        <view>
          <image
            class="titleImg"
            src="../../../static/image/home/icon_line_vertical.png"
          />
        </view>
        <view>刷卡费率</view>
      </view>
      <!-- 费率政策内容 -->
      <view class="" v-for="(_item, index) in agentPolicyCostInfo" :key="index">
        <!-- POS -->
        <view class="" v-if="_item.payTypeCode == 'POS'">
          <view class="" v-if="_item.cardType == '1'">
            <view class="flex list-item">
              <view class="flex-1"> T+0（%） </view>
              <view class="config_content">
                <u-input
                  class="config_input"
                  :border="border"
                  placeholder=""
                  v-model="_item.t0Rate"
                  :disabled="isDisabled"
                ></u-input>
              </view>
            </view>
          </view>
          <view class="" v-if="_item.cardType == '2'">
            <view class="flex list-item">
              <view class="flex-1"> T+1（%） </view>
              <view class="config_content">
                <u-input
                  class="config_input"
                  :border="border"
                  placeholder=""
                  v-model="_item.t1Rate"
                  :disabled="isDisabled"
                ></u-input>
              </view>
            </view>
            <view class="flex list-item">
              <view class="flex-1"> T+0提现费 </view>
              <view class="config_content">
                <u-input
                  class="config_input"
                  :border="border"
                  placeholder=""
                  v-model="_item.cashFee"
                  :disabled="isDisabled"
                ></u-input>
              </view>
            </view>
          </view>
        </view>
      </view>
    </view>
    <!-- 扫码费率 -->
    <view class="rateConfig_box">
      <view class="rateConfig_title">
        <view>
          <image
            class="titleImg"
            src="../../../static/image/home/icon_line_vertical.png"
          />
        </view>
        <view>扫码费率</view>
      </view>
      <!-- 费率政策内容 -->
      <view class="" v-for="(_item, index) in agentPolicyCostInfo" :key="index">
        <view class="" v-if="_item.payTypeCode == 'WX'">
          <view class="flex list-item">
            <view class="flex-1"> 微信支付 </view>
            <view class="config_content">
              <u-input
                class="config_input"
                placeholder=""
                :border="border"
                v-model="_item.t1Rate"
                :disabled="isDisabled"
              ></u-input>
            </view>
          </view>
        </view>
        <view class="" v-if="_item.payTypeCode == 'ZFB'">
          <view class="flex list-item">
            <view class="flex-1"> 支付宝支付 </view>
            <view class="config_content">
              <u-input
                class="config_input"
                placeholder=""
                :border="border"
                v-model="_item.t1Rate"
                :disabled="isDisabled"
              ></u-input>
            </view>
          </view>
        </view>
        <view class="" v-if="_item.payTypeCode == 'YL_CODE_LESS'">
          <view class="flex list-item">
            <view class="flex-1"> 银联二维码（≤1000元） </view>
            <view class="config_content">
              <u-input
                class="config_input"
                placeholder=""
                :border="border"
                v-model="_item.t1Rate"
                :disabled="isDisabled"
              ></u-input>
            </view>
          </view>
        </view>
        <view class="" v-if="_item.payTypeCode == 'YL_CODE_MORE'">
          <view class="flex list-item">
            <view class="flex-1"> 银联二维码（＞1000元） </view>
            <view class="config_content">
              <u-input
                class="config_input"
                placeholder=""
                :border="border"
                v-model="_item.t1Rate"
                :disabled="isDisabled"
              ></u-input>
            </view>
          </view>
        </view>
      </view>
    </view>
    <!-- 操作按钮 -->
    <view class="handle">
      <u-button @click="handleAddEdit" class="anniu"> 提交 </u-button>
    </view>
  </view>
</template>

<script>
export default {
  data() {
    return {
      agentPolicyCostInfo: [],
      border: true, //input边框
      isDisabled: false, // input框禁用
      merchantNo: '', //商户编号
    };
  },
  // onLoad自带一个参数
  onLoad(options) {
    console.log('options----->122', options);
    // 获取商户编号
    this.merchantNo = options.merchantNo;
    // 根据商户编号查询商户费率信息
    this.getQueryMerRateChangeApplyList();
  },
  mounted() {},
  methods: {
    // 根据商户编号查询商户费率信息
    async getQueryMerRateChangeApplyList() {
      const result = await this.$zx.common_api.getQueryMerRateChangeApply(
        this.merchantNo
      );
      console.log('getQueryMerRateChangeApply----->92', result);
      // 遍历数组列表过滤NFC和移动数据
      const newCurrMerRateInfo = result.currMerRateInfo.filter((item) => {
        return item.payTypeCode !== 'NFC' && item.payTypeCode !== 'MOBILE_PAY';
      });
      // 赋值给agentPolicyCostInfo
      this.agentPolicyCostInfo = newCurrMerRateInfo;
      console.log(
        'this.agentPolicyCostInfo----->169',
        this.agentPolicyCostInfo
      );
    },
    // 提交修改返现事件
    async handleAddEdit() {
      // 找到 cardType 为 '2' 的对象
      const type2Item = this.agentPolicyCostInfo.find(
        (item) => item.cardType === '2'
      );
      console.log('type2Item----->183', type2Item);
      if (type2Item) {
        // 将 cardType 为 '2' 的 changeCashFee 赋值给所有 cardType 为 '1' 的对象
        this.agentPolicyCostInfo.forEach((item) => {
          if (item.cardType === '1') {
            item.cashFee = type2Item.cashFee;
            item.t0Rate = type2Item.t0Rate;
            item.t1Rate = type2Item.t1Rate;
          }
        });
      }
      const type2Item2 = this.agentPolicyCostInfo.forEach((item) => {
        if (
          item.payTypeCode == 'WX' ||
          item.payTypeCode === 'ZFB' ||
          item.payTypeCode === 'YL_CODE_LESS' ||
          item.payTypeCode === 'YL_CODE_MORE'
        ) {
          item.t0Rate = item.t1Rate;
        }
      });
      console.log('type2Item2----->218', type2Item2);
      // 创建一个新的数组，将t0Rate映射为后端规定的changeT0Rate参数名
      const payload = this.agentPolicyCostInfo.map((item) => ({
        cardType: item.cardType, //卡类型
        changeCashFee: item.cashFee, //修改后的提现手续费
        changeT0Rate: item.t0Rate, //修改后的T0费率
        changeT1Rate: item.t1Rate, //修改后的T1费率
        payTypeCode: item.payTypeCode, //支付类型编码
      }));
      console.log('payload----->168', payload);
      // 调用修改数据接口
      const result = await this.$zx.common_api.editUpdateMerPosRate(
        this.merchantNo,
        payload
      );
      console.log('editUpdateMerPosRate----->176', result);
      // 修改成功后提示
      uni.showToast({
        title: '修改成功',
        duration: 1000,
      });
      // 延迟1秒跳转回返现配置页面
      // setTimeout(() => {
      //   this.$u.route({
      //     url: 'pages/main/merchant/merchant-list',
      //     params: {
      //       agentNo: this.agentNo, // 传递代理商编号
      //     },
      //   });
      // }, 1000);
    },
  },
};
</script>

<style scoped lang="scss">
.container {
  .container_box {
    background-color: white;
    margin-top: 30rpx;
    font-size: 28rpx;
  }
  .container__title {
    font-size: 28rpx;
    font-weight: bold;
    padding: 20rpx 20rpx;
  }
}
.bg {
  background-color: #f6f6f6;
  padding: 10rpx 20rpx;
  border-radius: 10rpx;
}
// 内容样式
::v-deep .list-item {
  align-items: center;
  padding: 10rpx 24rpx;
  .config_content {
    display: flex;
    align-items: center;
    .config_input {
      width: 100rpx;
      height: 60rpx;
      display: flex;
      align-items: center;
      background: #f6f6f6;
      border: 0rpx solid #dcdfe6;
      .u-input__input {
        text-align: center;
        font-size: 28rpx;
      }
    }
    text {
      margin-left: 10rpx;
    }
  }
}
// 按钮
::v-deep .handle {
  padding: 40rpx 0rpx;
  display: flex;
  position: fixed;
  bottom: 40rpx;
  width: 100%;
  .anniu {
    width: 100%;
    color: white;
    background: #9d60ff;
    padding: 44rpx 0rpx;
    margin: 0rpx 40rpx;
    border-radius: 20rpx;
  }
}

// 费率
.rateConfig {
  .rateConfig_box {
    margin: 30rpx;
    padding: 30rpx;
    border-radius: 8px;
    background: white;
    .rateConfig_title {
      display: flex;
      vertical-align: middle;
      font-weight: bold;
      .titleImg {
        width: 15rpx;
        height: 40rpx;
        margin-right: 10rpx;
      }
    }
  }
}
</style>
